<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.memberName"
          clearable
          placeholder="會員名稱"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.appellation"
          clearable
          placeholder="称呼"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.contactNumber"
          clearable
          placeholder="聯繫電話"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <template slot="left">
          <el-button
            class="filter-item"
            size="mini"
            type="primary"
            @click="openDealWith"
          >
            处理
          </el-button>
        </template>
      </crudOperation>
      <el-dialog
        title="处理意见"
        :visible.sync="dialogVisible"
        width="30%"
      >
        <el-input v-model="dealMessage" type="textarea" :rows="4" placeholder="请输入内容" />
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="800px"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="100px" />
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        style="width: 100%;"
        stripe
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="memberId" label="会员id"       />
        <el-table-column prop="memberName" label="會員名稱"       />
        <el-table-column prop="appellation" label="称呼"       />
        <el-table-column prop="messageType" label="反馈类型"      >
          <template slot-scope="scope">
            {{ dict.label.messageType[scope.row.messageType] }}
          </template>
        </el-table-column>
        <el-table-column prop="message" label="反馈内容"       />
        <el-table-column prop="contactNumber" label="联系电话"       />
        <el-table-column prop="dealWith" label="是否处理"      >
          <template slot-scope="scope">
            <span>{{ scope.row.dealWith === true ? '是' : '否' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="处理备注"       />
        <el-table-column prop="createTime" label="创建时间"       />
        <el-table-column prop="updatedAt	" label="更新时间"       />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudMemberMessage from '@/api/msg/memberMessage'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'

const defaultForm = {
  memberId: null,
  memberName: null,
  appellation: null,
  contactNumber: null,
  message: null,
  messageType: null
}
export default {
  name: 'Message',
  components: { pagination, crudOperation, rrOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['messageType'],
  cruds() {
    return CRUD({
      title: '用户消息', url: 'feedback/page', sort: 'id:desc', crudMethod: { ...crudMemberMessage },
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: false,
        reset: true
      }
    })
  },
  data() {
    return {
      dealMessage: '',
      dialogVisible: false,
      permission: {
        add: ['admin', 'memberMessage:add'],
        edit: ['admin', 'memberMessage:edit'],
        del: ['admin', 'memberMessage:del']
      },
      rules: {},
      queryTypeOptions: [
        { key: 'memberName', display_name: '會員名稱' },
        { key: 'appellation', display_name: '称呼' },
        { key: 'messageType', display_name: '消息类型' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    openDealWith() {
      const currentSelect = this.$refs.table.selection
      if (currentSelect.length === 0) {
        this.$message.warning('请选择处理反馈')
        return
      }
      if (currentSelect.length !== 0) {
        this.dialogVisible = true
      }
    },
    submit() {
      const currentSelect = this.$refs.table.selection
      const ids = []
      for (let i = 0; i < currentSelect.length; i++) {
        ids.push(currentSelect[i].id)
      }
      const data = { 'ids': ids, message: this.dealMessage }
      crudMemberMessage.dealWith(data).then(() => {
        this.$message.success('处理成功')
        this.dealMessage = ''
        this.dialogVisible = false
      })
    }
  }
}
</script>

<style scoped>

</style>
